package us.mooresr.geartrack.client.core.desktop;

import java.util.List;


import us.mooresr.geartrack.shared.model.DatastoreObject;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class ContentWidget extends Composite {
	
	@UiField Label contentTitle;
	@UiField ScrollPanel listPanel;
	@UiField HTMLPanel contentPanel;
	@UiField Button button1;
	@UiField Button button2;
	@UiField Button button3;
	private ContentWidgetEventHandler handler;
	private ListDataProvider<DatastoreObject> dataProvider;
	private SingleSelectionModel<DatastoreObject> selectionModel;
	
	CellList<DatastoreObject> cellList;
	
	private static ContentWidgetUiBinder uiBinder = GWT
			.create(ContentWidgetUiBinder.class);

	interface ContentWidgetUiBinder extends UiBinder<Widget, ContentWidget> {
	}
	
	static class ListCell extends AbstractCell<DatastoreObject> {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				DatastoreObject value, SafeHtmlBuilder sb) {
			
			String content;
			
			if (value == null) {
				content = "<null>";
			} else if (value.toString() == null) {
				content = "<null>";
			} else {
				content = value.toString();
			}
			
			sb.appendEscaped(content);
			
			
		}
		
	}


	public ContentWidget() {

		
		initWidget(uiBinder.createAndBindUi(this));
		
		contentTitle.setText("Content Area Title");
		
		ProvidesKey<DatastoreObject> keyProvider = new ProvidesKey<DatastoreObject>() {

			@Override
			public Object getKey(DatastoreObject item) {
				if (item == null) {
					return null;
				} else {
					return item.getId();
				}
			}


			
		};

		
		ListCell cell = new ListCell();
		selectionModel = new SingleSelectionModel<DatastoreObject>(keyProvider);
		cellList = new CellList<DatastoreObject>(cell, keyProvider);
		
		dataProvider = new ListDataProvider<DatastoreObject>();
		dataProvider.addDataDisplay(cellList);
		
		cellList.setSelectionModel(selectionModel);
		
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				DatastoreObject selected = selectionModel.getSelectedObject();
				
				if (selected != null) {
					if (handler != null) {
						handler.onSelection(selected);
					}

				}
				
			}
			
		});
		
		listPanel.add(cellList);
	}
	
	/**
	 * Called by the containing view, to tell the widget where to send button events.
	 * 
	 * @param handler
	 */
	public void setHandler(ContentWidgetEventHandler handler) {
		this.handler = handler;
	}
	

	@UiHandler("button1")
	void onButton1Clicked(ClickEvent event) {
		if (handler != null) {
			handler.onButton1Click();
		}
	}
	
	@UiHandler("button2")
	void onButton2Clicked(ClickEvent event) {
		if (handler != null) {
			handler.onButton2Click();
		}
	}
	
	@UiHandler("button3")
	void onButton3Clicked(ClickEvent event) {
		if (handler != null) {
			handler.onButton3Click();
		}
	}
	
	public void setTitle(String title) {
		contentTitle.setText(title);
	}

	public void setListContents(List<? extends DatastoreObject> newList) {
		List<DatastoreObject> list = dataProvider.getList();
		list.clear();
		list.addAll(newList);

		
	}
	
	public void addItemToList(DatastoreObject item) {
		List<DatastoreObject> list = dataProvider.getList();
		list.add(item);
		
	}
	
	public void removeItemFromList(DatastoreObject item) {
		List<DatastoreObject> list = dataProvider.getList();
		
		DatastoreObject objectToRemove = null;
		
		for (DatastoreObject o : list) {
			if (o.getId().equals(item.getId())) {
				objectToRemove = o;
				break;
			}
		}
		
		if (objectToRemove != null) {
			list.remove(objectToRemove);
		}
	}
	
	public void updateListItem(DatastoreObject item) {
		List<DatastoreObject> list = dataProvider.getList();
		DatastoreObject objectToUpdate = null;
		
		for (DatastoreObject o : list) {
			if (o.getId().equals(item.getId())) {
				objectToUpdate = o;
				break;
			}
		}
		
		if (objectToUpdate != null) {
			list.remove(objectToUpdate);
			list.add(item);
		}
	}
	
	public void setContent(Widget contentWidget) {
		contentPanel.clear();
		contentPanel.add(contentWidget);
	}
	
	public void showButton(int which) {
		Button button;
		
		switch (which) {
		case 1: 
			button = button1;
			break;
		case 2:
			button = button2;
			break;
		case 3:
			button = button3;
			break;
		default:
			return;
		}
		
		button.setVisible(true);
	}
	
	public void hideButton(int which) {
		Button button;
		
		switch (which) {
		case 1: 
			button = button1;
			break;
		case 2:
			button = button2;
			break;
		case 3:
			button = button3;
			break;
		default:
			return;
		}
		
		button.setVisible(false);
	}
	
	public void disableButton(int which) {
		Button button;
		
		switch (which) {
		case 1: 
			button = button1;
			break;
		case 2:
			button = button2;
			break;
		case 3:
			button = button3;
			break;
		default:
			return;
		}
		
		button.setEnabled(false);
	}
	
	public void enableButton(int which) {
		Button button;
		
		switch (which) {
		case 1: 
			button = button1;
			break;
		case 2:
			button = button2;
			break;
		case 3:
			button = button3;
			break;
		default:
			return;
		}
		
		button.setEnabled(true);
	}
	
	
	public void setButtonText(int which, String text) {
		Button button;
		
		switch (which) {
		case 1: 
			button = button1;
			break;
		case 2:
			button = button2;
			break;
		case 3:
			button = button3;
			break;
		default:
			return;
		}
		
		button.setText(text);
	}
	
	public void clearSelection() {
		DatastoreObject selectedObject = selectionModel.getSelectedObject();
		if (selectedObject != null) {
			selectionModel.setSelected(selectedObject, false);
		}
	}
	

}
